package com.hy.controller;

import com.hy.pojo.Products;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2022/11/1
 * Time: 19:10
 */
@RestController
@RequestMapping("/page")
public class PageController {
    @Resource
    private RestTemplate restTemplate;
    
    @Resource
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/query/{id}")
    public Products getProductById(@PathVariable int id){
        List<ServiceInstance> instances = discoveryClient.getInstances("hy-service-product");
        System.out.println(instances);
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        System.out.println(host + " " + port);
        String url = "http://"+host +":" + port +"/product/query/" + id;
        Products products = restTemplate.getForObject(url, Products.class);
        return products;
    }
}
